package com.idealighter.game.server.db.datadb.dao;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.core.constant.group.GroupApplyState;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberApplyDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupMemberApplyDomainExample;
import com.idealighter.game.server.db.datadb.mapper.RoomCardGroupMemberApplyMapper;
import com.idealighter.utils.check.EmptyUtil;
import com.idealighter.utils.time.TimeUtil;

import java.util.List;

@Singleton
public class RoomCardGroupMemberApplyDao {

  private RoomCardGroupMemberApplyMapper mapper;

  @Inject
  public RoomCardGroupMemberApplyDao(RoomCardGroupMemberApplyMapper mapper) {
    this.mapper = mapper;
  }

  /**
   * 获取申请列表 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:24:01
   * @param groupId 群id.
   * @param state 状态.
   * @return 申请列表.
   */
  public List<RoomCardGroupMemberApplyDomain> selectApplyList(int groupId, Byte state) {
    RoomCardGroupMemberApplyDomainExample example = new RoomCardGroupMemberApplyDomainExample();
    RoomCardGroupMemberApplyDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId);

    if (state != null) {
      criteria.andStateEqualTo(state);
    }

    return mapper.selectByExample(example);
  }

  /**
   * 处理申请 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:24:30
   * @param id 申请id.
   * @param state 处理状态.
   * @return 影响条数.
   */
  public int updateForDeal(int id, Byte state) {
    RoomCardGroupMemberApplyDomain domain = new RoomCardGroupMemberApplyDomain();
    domain.setId(id);
    domain.setState(state);
    domain.setDealTime(TimeUtil.now());
    return mapper.updateByPrimaryKeySelective(domain);
  }

  public int insertSelective(RoomCardGroupMemberApplyDomain domain) {
    return mapper.insertSelective(domain);
  }

  public RoomCardGroupMemberApplyDomain selectByPrimaryKey(int id) {
    return mapper.selectByPrimaryKey(id);
  }

  /**
   * 是否有未处理的消息 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:24:56
   * @param groupId 群id.
   * @return 是否含有.
   */
  public boolean hasMessage(Integer groupId) {
    RoomCardGroupMemberApplyDomainExample example = new RoomCardGroupMemberApplyDomainExample();
    RoomCardGroupMemberApplyDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andStateEqualTo(GroupApplyState.NOT_DEAL);

    return mapper.countByExample(example) > 0;
  }

  /**
   * 删除所有请求 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:25:25
   * @param groupId 群id.
   * @return 影响条数.
   */
  public int deleteAllByGroupId(Integer groupId) {
    RoomCardGroupMemberApplyDomainExample example = new RoomCardGroupMemberApplyDomainExample();
    RoomCardGroupMemberApplyDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId);

    return mapper.deleteByExample(example);
  }

  public int delete(Integer applyId) {
    return mapper.deleteByPrimaryKey(applyId);
  }

  /**
   * 获取我的申请 .
   *
   * @author abin
   * @date 2018年5月24日 下午1:01:59
   * @param groupId groupid.
   * @param playerId 玩家id.
   * @return 申请.
   */
  public RoomCardGroupMemberApplyDomain selectMyApply(int groupId, long playerId) {
    RoomCardGroupMemberApplyDomainExample example = new RoomCardGroupMemberApplyDomainExample();
    RoomCardGroupMemberApplyDomainExample.Criteria criteria = example.createCriteria();
    criteria.andGroupIdEqualTo(groupId).andPlayerIdEqualTo(playerId);

    RoomCardGroupMemberApplyDomain result = null;

    List<RoomCardGroupMemberApplyDomain> memberApplyDomains = mapper.selectByExample(example);

    if (EmptyUtil.listIsNotEmpty(memberApplyDomains)) {
      result = memberApplyDomains.get(0);
    }
    return result;
  }

}
